//
//  MessageTableViewCell.m
//  BDD_TWO
//
//  Created by XTW on 2023/4/27.
//

#import "MessageTableViewCell.h"
#import <WFChatClient/WFCChatClient.h>
#import "WFCUUtilities.h"
#import "WFCUConfigManager.h"

@interface MessageTableViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *lastMessageLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *unreadLabel;

@end

@implementation MessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setInfo:(WFCCConversationInfo *)info {
    _info = info;
    if (info.unreadCount.unread == 0) {
        self.unreadLabel.hidden = YES;
    } else {
        self.unreadLabel.hidden = NO;
        self.unreadLabel.text = [NSString stringWithFormat:@"%d", info.unreadCount.unread];
    }
    [self update:info.conversation];
    self.timeLabel.text = [WFCUUtilities formatTimeLabel:info.timestamp];
    
    if (info.isTop) {
        [self.contentView setBackgroundColor:ViewBackgroundColor];
    } else {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)update:(WFCCConversation *)conversation {
    self.nameLabel.textColor = [WFCUConfigManager globalManager].textColor;
    if(conversation.type == Single_Type) {
        WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:conversation.target refresh:NO];
        if(userInfo.userId.length == 0) {
            userInfo = [[WFCCUserInfo alloc] init];
            userInfo.userId = conversation.target;
        }
        [self updateUserInfo:userInfo];
    }  else {
        self.nameLabel.text = @"Chatroom";
    }
    
    self.lastMessageLabel.attributedText = nil;
    if (_info.draft.length) {
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:WFCString(@"[Draft]") attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];
        
        NSError *__error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[_info.draft dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options:kNilOptions
                                                                     error:&__error];
        
        NSString *text = _info.draft;
        if (!__error) {
            //兼容android/web端
            if([dictionary[@"content"] isKindOfClass:[NSString class]]) {
                text = dictionary[@"content"];
            } else if([dictionary[@"text"] isKindOfClass:[NSString class]]) {
                text = dictionary[@"text"];
            }
        }
        
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:text]];

        if (_info.conversation.type == Group_Type && _info.unreadCount.unreadMentionAll + _info.unreadCount.unreadMention > 0) {
            NSMutableAttributedString *tmp = [[NSMutableAttributedString alloc] initWithString:@"[MentionYou]" attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];
            [tmp appendAttributedString:attString];
            attString = tmp;
        }
        self.lastMessageLabel.attributedText = attString;
    } else if (_info.lastMessage.direction == MessageDirection_Receive && (_info.conversation.type == Group_Type || _info.conversation.type == Channel_Type)) {
        NSString *groupId = nil;
        if (_info.conversation.type == Group_Type) {
            groupId = _info.conversation.target;
        }
        WFCCUserInfo *sender = [[WFCCIMService sharedWFCIMService] getUserInfo:_info.lastMessage.fromUser inGroup:groupId refresh:NO];
        if (sender.friendAlias.length && ![_info.lastMessage.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
            self.lastMessageLabel.text = [NSString stringWithFormat:@"%@:%@", sender.friendAlias, _info.lastMessage.digest];
        } else if (sender.groupAlias.length && ![_info.lastMessage.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
            self.lastMessageLabel.text = [NSString stringWithFormat:@"%@:%@", sender.groupAlias, _info.lastMessage.digest];
        } else if (sender.displayName.length && ![_info.lastMessage.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
            self.lastMessageLabel.text = [NSString stringWithFormat:@"%@:%@", sender.displayName, _info.lastMessage.digest];
        } else {
            self.lastMessageLabel.text = _info.lastMessage.digest;
        }
        
        if (_info.conversation.type == Group_Type && _info.unreadCount.unreadMentionAll + _info.unreadCount.unreadMention > 0) {
            NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"[MentionYou]" attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];
            if (self.lastMessageLabel.text.length) {
                [attString appendAttributedString:[[NSAttributedString alloc] initWithString:self.lastMessageLabel.text]];
            }
            
            self.lastMessageLabel.attributedText = attString;
        }
    } else {
        self.lastMessageLabel.text = _info.lastMessage.digest;
    }
}

- (void)updateUserInfo:(WFCCUserInfo *)userInfo {
    if ([userInfo.userId isEqualToString:WFChatCustomer]) {
        self.nameLabel.text = @"客服";
        self.headerImageView.image = [UIImage imageNamed:@"kefu"];
    } else {
        if (userInfo.friendAlias.length > 0) {
            self.nameLabel.text = userInfo.friendAlias;
        } else if(userInfo.displayName.length > 0) {
            self.nameLabel.text = userInfo.displayName;
            self.nameLabel.text = [NSString stringWithFormat:@"%@-%@",userInfo.displayName, userInfo.mobile];
        } else {
            self.nameLabel.text = [NSString stringWithFormat:@"user<%@>", self.info.conversation.target];
        }
        self.headerImageView.image = [UIImage imageNamed:@"defaultHeader"];
    }

}

@end
